/*
 *    Copyright (c) 2018-2025, proj All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the proj developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: proj
 */

package com.ykht.proj.coupon.controller;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ykht.proj.admin.api.entity.SysUser;
import com.ykht.proj.common.core.util.R;
import com.ykht.proj.common.log.annotation.SysLog;
import com.ykht.proj.common.security.service.ProjUser;
import com.ykht.proj.common.security.util.SecurityUtils;
import com.ykht.proj.coupon.Enum.UserTypeEnum;
import com.ykht.proj.coupon.entity.CnBrand;
import com.ykht.proj.coupon.entity.CnCompany;
import com.ykht.proj.coupon.service.CnCompanyService;
import com.ykht.proj.coupon.vo.CnCompanyVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;


/**
 * 公司表
 *
 * @author proj code generator
 * @date 2020-11-17 14:05:06
 */
@RestController
@AllArgsConstructor
@RequestMapping("/cncompany")
@Api(value = "cncompany", tags = "公司表管理")
@Slf4j
public class CnCompanyController {

	private final CnCompanyService cnCompanyService;

	/**
	 * 根据角色类型 获取后台的公司列表 分页查询 分页查询
	 *
	 * @param page      分页对象
	 * @param cnCompany 公司表
	 * @return
	 */
	@ApiOperation(value = "分页查询", notes = "分页查询")
	@GetMapping("/page")
//	@PreAuthorize("@pms.hasPermission('coupon_cncompany_view')")
	public R<IPage<CnCompanyVo>> getCnCompanyPage(Page page, CnCompany cnCompany) {
		log.info("->根据角色类型 获取后台的公司列表 分页查询:{}", JSONUtil.toJsonStr(cnCompany));
		IPage<CnCompanyVo> companyVoList = cnCompanyService.getCnCompanyPage(page, cnCompany);
		return R.ok(companyVoList);
	}

	@ApiOperation(value = "查询所有公司", notes = "查询所有公司")
	@GetMapping("/getAll")
//    @PreAuthorize("@pms.hasPermission('coupon_cncompany_view')" )
	public R getAll() {
		ProjUser user = SecurityUtils.getUser();
		return R.ok(cnCompanyService.list(Wrappers.lambdaQuery(CnCompany.class)
				.eq(CnCompany::getDisplay,0)
				.eq(user.getUserType()==UserTypeEnum.company.getValue(),CnCompany::getId,user.getSjId())));
	}

	/**
	 * 通过id查询公司表
	 *
	 * @param id id
	 * @return R
	 */
	@ApiOperation(value = "通过id查询", notes = "通过id查询")
	@GetMapping("/{id}")
	@PreAuthorize("@pms.hasPermission('coupon_cncompany_view')")
	public R getById(@PathVariable("id") Long id) {
		return R.ok(cnCompanyService.getById(id));
	}

	/**
	 * 新增公司表
	 *
	 * @param cnCompanyVo 公司表
	 * @return R
	 */
	@ApiOperation(value = "新增公司表", notes = "新增公司表")
	@SysLog("新增公司表")
	@PostMapping
	@PreAuthorize("@pms.hasPermission('coupon_cncompany_add')")
	public R save(@RequestBody CnCompanyVo cnCompanyVo) throws Exception {
		log.info("->新增公司表:{}", JSONUtil.toJsonStr(cnCompanyVo));
		return cnCompanyService.saveAndUser(cnCompanyVo);
	}

	/**
	 * 修改公司表
	 *
	 * @param cnCompanyVo 公司表
	 * @return R
	 */
	@ApiOperation(value = "修改公司表", notes = "修改公司表")
	@SysLog("修改公司表")
	@PutMapping
//	@PreAuthorize("@pms.hasPermission('coupon_cncompany_edit')")
	public R updateById(@RequestBody CnCompanyVo cnCompanyVo) throws Exception {
		log.info("->修改公司表:{}", JSONUtil.toJsonStr(cnCompanyVo));
		return cnCompanyService.updateAndUser(cnCompanyVo);
	}

	/**
	 * 通过id删除公司表
	 *
	 * @param id id
	 * @return R
	 */
	@ApiOperation(value = "通过id删除公司表", notes = "通过id删除公司表")
	@SysLog("通过id删除公司表")
	@DeleteMapping("/{id}")
	@PreAuthorize("@pms.hasPermission('coupon_cncompany_del')")
	public R removeById(@PathVariable Long id) {
		return R.ok(cnCompanyService.removeById(id));
	}

	/**
	 * 上传
	 *
	 * @param file
	 * @return
	 */
	@ApiOperation(value = "上传文件")
	@PostMapping("uploadFile")
	public R uploadFile(@RequestParam("file") MultipartFile file) {
		return R.ok(cnCompanyService.uploadFile(file));
	}

/*
	@PostMapping("getByLoginId")
	public R<Long> getByLoginId(@RequestBody String loginId) {
		System.out.println("==========================");
		return R.ok(cnCompanyService.getOne(Wrappers.lambdaQuery(CnCompany.class)
				.eq(CnCompany::getLoginId, Long.valueOf(loginId))).getId());
	}
*/


	/**
	 * 公司上下架
	 *
	 * @param cnCompanyVo 公司表
	 * @return R
	 */
	@ApiOperation(value = "公司上下架", notes = "公司上下架")
	@SysLog("公司上下架")
	@PostMapping("/updateByDisplay")
	public R updateByDisplay(@RequestBody CnCompanyVo cnCompanyVo) throws Exception {
		log.info("->公司上下架:{}", JSONUtil.toJsonStr(cnCompanyVo));
		return cnCompanyService.updateByDisplay(cnCompanyVo);
	}

}
